from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin

class AuthMiddleware(MiddlewareMixin):

    def process_request(self, request):
        # 排除那些不需要登录就能访问的页面
        if request.path_info in ["/login/", "/image/code/"]:
            return None

        # 读取当前访问的用户的 session 信息
        info_dict = request.session.get("info")
        if info_dict:
            return None  # 如果已登录，继续请求

        # 没有登录过，重新回到登录页面
        return redirect('/login/')



